package edu.ccut.saturn.component.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import edu.ccut.saturn.component.ComponentManager;
import edu.ccut.saturn.component.IComponentInfo;
import edu.ccut.saturn.component.IDebugComponentInfo;
import edu.ccut.saturn.component.IDebugLogMessage;
import edu.ccut.saturn.component.INodeInfo;
import edu.ccut.saturn.component.IParameterContext;
import edu.ccut.saturn.component.SaturnComponentException;
import edu.ccut.saturn.component.utils.DateUtils;

public class SaturnDebugComponentInfo implements IDebugComponentInfo {

	private IParameterContext afterComponentParameterContext;

	private IParameterContext beforeComponentParameterContext;

	private List<IDebugComponentInfo> children = new ArrayList<IDebugComponentInfo>();

	private List<IDebugLogMessage> logMessages = new ArrayList<IDebugLogMessage>();

	private INodeInfo nodeInfo;

	private long startTime = -1;

	private long endTime = -1;

	public String getName() {

		if (this.nodeInfo != null) {
			return this.nodeInfo.getName();
		}

		return "Component";
	}

	public long getStartTime() {
		return startTime;
	}

	public void setStartTime(long startTime) {
		this.startTime = startTime;
	}

	public long getEndTime() {
		return endTime;
	}

	public void setEndTime(long endTime) {
		this.endTime = endTime;
	}

	public IParameterContext getAfterComponentParameterContext() {
		return afterComponentParameterContext;
	}

	public void setAfterComponentParameterContext(
			IParameterContext afterComponentParameterContext) {

		if (afterComponentParameterContext != null) {
			this.afterComponentParameterContext = afterComponentParameterContext
					.newParameterContext();
		}
	}

	public IParameterContext getBeforeComponentParameterContext() {
		return beforeComponentParameterContext;
	}

	public void setBeforeComponentParameterContext(
			IParameterContext beforeComponentParameterContext) {

		if (beforeComponentParameterContext != null) {
			this.beforeComponentParameterContext = beforeComponentParameterContext
					.newParameterContext();
		}
	}

	public List<IDebugComponentInfo> getChildren() {
		return children;
	}

	public void addChild(IDebugComponentInfo child) {
		this.children.add(child);
	}

	public INodeInfo getNodeInfo() {
		return nodeInfo;
	}

	public void setNodeInfo(INodeInfo nodeInfo) {
		this.nodeInfo = nodeInfo;
	}

	public IComponentInfo getComponentInfo() {

		if (this.nodeInfo != null && this.nodeInfo.getKey() != null) {

			try {
				return ComponentManager.getInstance().getComponentInfo(
						this.nodeInfo.getKey());

			} catch (SaturnComponentException e) {
				// e.printStackTrace();
			}
		}

		return null;
	}

	public boolean isEnd() {
		return this.endTime > 0;
	}

	public String getEndTimeStr() {
		return DateUtils.getSysteTimeMillisecond(new Date(this.endTime));
	}

	public String getStartTimeStr() {
		return DateUtils.getSysteTimeMillisecond(new Date(this.startTime));
	}

	public void addLogMessage(IDebugLogMessage debugLogMessage) {
		this.logMessages.add(debugLogMessage);
	}

	public List<IDebugLogMessage> getLogMessages() {
		return this.logMessages;
	}

//	public boolean hasException() {
//
//		if (this.logMessages != null) {
//
//			for (IDebugLogMessage message : this.logMessages) {
//
//				if (message.hasException()) {
//					return true;
//				}
//			}
//
//			for (IDebugComponentInfo componentInfo : this.children) {
//
//				if (componentInfo.hasException()) {
//					return true;
//				}
//			}
//		}
//		return false;
//	}

	public boolean hasLevelMessage(String level) {

		if (level != null && !"".equals(level)) {

			if (this.logMessages != null) {

				for (IDebugLogMessage message : this.logMessages) {

					if (level.equals(message.getLevel())) {
						return true;
					}
				}

				for (IDebugComponentInfo componentInfo : this.children) {

					if (componentInfo.hasLevelMessage(level)) {
						return true;
					}
				}
			}
		}
		return false;
	}
}
